#! /bin/sh
# automatically run by "lb config"

set -x

# we require building from git
if ! git rev-parse --is-inside-work-tree; then
    echo "${PWD} is not a Git tree. Exiting."
    exit 1
fi

. config/amnesia
if [ -e config/amnesia.local ] ; then
   . config/amnesia.local
fi

export LB_BOOTSTRAP_INCLUDE='eatmydata'

# sanity checks
if grep -qs -E '^Pin:\s+release\s+.*a=' config/chroot_apt/preferences ; then
    echo "Found unsupported a= syntax in config/chroot_apt/preferences,"
    echo "use n= instead. Exiting."
    exit 1
fi
if grep -qs -E '^Pin:\s+release\s+.*o=Debian Backports' \
	config/chroot_apt/preferences ; then
    echo "Found unsupported 'o=Debian Backports' syntax,"
    echo "in config/chroot_apt/preferences. Use o=Debian instead. Exiting."
    exit 1
fi

# init variables
RUN_LB_CONFIG="lb config noauto"

# init config/ with defaults for the target distribution
$RUN_LB_CONFIG --distribution jessie ${@}

# set up everything for time-based snapshots:
apt-snapshots-serials prepare-build

DEBIAN_MIRROR="$(apt-mirror debian)"
DEBIAN_SECURITY_MIRROR="$(apt-mirror debian-security)"
TORPROJECT_MIRROR="$(apt-mirror torproject)"

[ -n "$DEBIAN_MIRROR" ]          || exit 1
[ -n "$DEBIAN_SECURITY_MIRROR" ] || exit 1
[ -n "$TORPROJECT_MIRROR" ]      || exit 1

perl -pi \
     -E \
       "s|^(deb(?:-src)?\s+)https?://ftp[.]us[.]debian[.]org/debian/?(\s+)|\$1$DEBIAN_MIRROR\$2| ; \
        s|^(deb(?:-src)?\s+)https?://deb[.]torproject[.]org/torproject[.]org/?(\s+)|\$1$TORPROJECT_MIRROR\$2|" \
    config/chroot_sources/*.chroot \
    || exit 1

# set Amnesia's general options
$RUN_LB_CONFIG \
   --verbose \
   --apt-recommends false \
   --backports false \
   --binary-images iso \
   --binary-indices false \
   --checksums none \
   --bootappend-live "${AMNESIA_APPEND}" \
   --bootstrap debootstrap \
   --bootstrap-config tails-build-jessie \
   --archive-areas "main contrib non-free" \
   --includes none \
   --iso-application="The Amnesic Incognito Live System" \
   --iso-publisher="https://tails.boum.org/" \
   --iso-volume="TAILS ${AMNESIA_FULL_VERSION}" \
   --memtest none \
   --mirror-binary              "$DEBIAN_MIRROR" \
   --mirror-bootstrap           "$DEBIAN_MIRROR" \
   --mirror-chroot              "$DEBIAN_MIRROR" \
   --mirror-binary-security     "$DEBIAN_SECURITY_MIRROR" \
   --mirror-chroot-security     "$DEBIAN_SECURITY_MIRROR" \
   --packages-lists="standard" \
   --tasks="standard" \
   --linux-packages="linux-image-${KERNEL_VERSION}" \
   --syslinux-menu vesamenu \
   --syslinux-splash data/splash.png \
   --syslinux-timeout 4 \
   --initramfs=live-boot \
   ${@}

# build i386 images on amd64 as well, include a bunch of kernels
hw_arch="`dpkg --print-architecture`"
if [ "$hw_arch" = i386 -o "$hw_arch" = amd64 ]; then
   $RUN_LB_CONFIG \
      --architecture i386 \
      --linux-flavours "686-unsigned" \
      ${@}
# build powerpc images on powerpc64 as well, include only powerpc kernel
elif [ "$hw_arch" = powerpc -o "$hw_arch" = powerpc64 ]; then
   $RUN_LB_CONFIG \
      --architecture powerpc \
      --linux-flavours powerpc \
      ${@}
fi

install -d config/chroot_local-includes/etc/amnesia/

# environment
TAILS_WIKI_SUPPORTED_LANGUAGES="$(ikiwiki-supported-languages ikiwiki.setup)"
[ -n "$TAILS_WIKI_SUPPORTED_LANGUAGES" ] || exit 16
echo "TAILS_WIKI_SUPPORTED_LANGUAGES='${TAILS_WIKI_SUPPORTED_LANGUAGES}'" \
   >> config/chroot_local-includes/etc/amnesia/environment

# version
echo "${AMNESIA_FULL_VERSION}" > config/chroot_local-includes/etc/amnesia/version
if git rev-list HEAD 2>&1 >/dev/null; then
   git rev-list HEAD | head -n 1 >> config/chroot_local-includes/etc/amnesia/version
fi
echo "live-build: `dpkg-query -W -f='${Version}\n' live-build`" \
   >> config/chroot_local-includes/etc/amnesia/version
# os-release
cat >> config/chroot_local-includes/etc/os-release <<EOF
TAILS_PRODUCT_NAME="Tails"
TAILS_VERSION_ID="$AMNESIA_VERSION"
EOF

# changelog
cp debian/changelog config/chroot_local-includes/usr/share/doc/amnesia/Changelog

# create readahead-list from squashfs.sort
if [ -e config/binary_rootfs/squashfs.sort ]; then
    mkdir -p config/chroot_local-includes/usr/share/amnesia
    sort -k2 -n -r config/binary_rootfs/squashfs.sort |
        cut -d' ' -f1 > config/chroot_local-includes/usr/share/amnesia/readahead-list
fi

# custom APT sources
tails-custom-apt-sources > config/chroot_sources/tails.chroot

# tails-transform-mirror-url and its dependencies
install -m 0755 \
   submodules/mirror-pool-dispatcher/bin/tails-transform-mirror-url \
   config/chroot_local-includes/usr/local/bin/
install -m 0755 -d config/chroot_local-includes/usr/local/lib/nodejs
install -m 0755 \
   submodules/mirror-pool-dispatcher/lib/js/mirror-dispatcher.js \
   config/chroot_local-includes/usr/local/lib/nodejs/

# aufs4-standalone
rm -rf config/chroot_local-includes/usr/src/aufs4-standalone
cp -a submodules/aufs4-standalone config/chroot_local-includes/usr/src/

# custom debootstrap script, setting some APT magic to log downloads:
patch \
    --follow-symlinks \
    --output=/usr/share/debootstrap/scripts/tails-build-jessie \
    /usr/share/debootstrap/scripts/jessie \
    data/debootstrap/scripts/jessie.patch
sed -i "s,%%topdir%%,$(pwd)," /usr/share/debootstrap/scripts/tails-build-jessie
